
package com.ostrich.a2104shopping.ui.home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ostrich.a2104shopping.R;
import com.ostrich.a2104shopping.databinding.ItemHomeCategoryBinding;
import com.ostrich.a2104shopping.databinding.ItemHomeGoodsBinding;
import com.ostrich.a2104shopping.ui.home.entity.CategoryEntity;
import com.ostrich.a2104shopping.ui.home.entity.GoodsEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/6 09:08
 * @Description : CategoryAdapter
 */
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.GoodsViewHolder> {
    private List<GoodsEntity> mGoodsEntities = new ArrayList<>();


    //追加数据
    public void setData(List<GoodsEntity> list){
        if(list == null || list.size() == 0){
            return;
        }
        mGoodsEntities.addAll(list);
        notifyDataSetChanged();
    }

    //刷新数据
    public void refreshData(List<GoodsEntity> list){
        if(list == null || list.size() == 0){
            return;
        }
        mGoodsEntities.clear();
        mGoodsEntities.addAll(list);
        notifyDataSetChanged();
    }

    //填充一条数据
    public void addData(GoodsEntity category){
        mGoodsEntities.add(category);
        notifyItemInserted(mGoodsEntities.size()-1);
    }
    //删除数据
    public void deleteData(int position){
        mGoodsEntities.remove(position);
        notifyItemRemoved(position);
    }


    //根据下标获得数据
    public GoodsEntity getDataByPosition(int position){
        if(position >= 0 && position <= mGoodsEntities.size()-1){
            return mGoodsEntities.get(position);
        }else{
            return null;
        }

    }








    @NonNull
    @Override
    public GoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemHomeGoodsBinding itemHomeGoodsBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_home_goods, parent, false);
        return new GoodsViewHolder(itemHomeGoodsBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsViewHolder holder, int position) {
        holder.mItemHomeGoodsBinding.setGoods(mGoodsEntities.get(position));



        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //position！！！！！
                if(mItemClickListener != null){
                    mItemClickListener.onItemClick(v,holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mGoodsEntities.size();
    }

    class  GoodsViewHolder extends RecyclerView.ViewHolder{
        private ItemHomeGoodsBinding mItemHomeGoodsBinding;


        public GoodsViewHolder(ItemHomeGoodsBinding mItemHomeGoodsBinding) {
            super(mItemHomeGoodsBinding.getRoot());
            this.mItemHomeGoodsBinding = mItemHomeGoodsBinding;
        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    private ItemClickListener mItemClickListener;


    public interface ItemClickListener{
        void onItemClick(View view, int position);
    }
}
